#!/usr/bin/env bash
# Summary: Detect the file that sets the current sbtenv version

set -e
test -n "${SBTENV_DEBUG}" && set -x

find_local_version_file() {
  local root="${1}"
  while [ -n "${root}" ]; do
    if [ -e "${root}/.sbt-version" ]; then
      echo "${root}/.sbt-version"
      exit
    fi
    root="${root%/*}"
  done
}

find_local_version_file "${SBTENV_DIR}"
[ "${SBTENV_DIR}" = "${PWD}" ] || find_local_version_file "${PWD}"

global_version_file="${SBTENV_ROOT}/version"

if [ -e "${global_version_file}" ]; then
  echo "${global_version_file}"
elif [ -e "${SBTENV_ROOT}/global" ]; then
  echo "${SBTENV_ROOT}/global"
elif [ -e "${SBTENV_ROOT}/default" ]; then
  echo "${SBTENV_ROOT}/default"
else
  echo "${global_version_file}"
fi

